Imports System.Windows.Forms

Public Class dlgType

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub txtID_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtID.TextChanged
        If db.getvalue("select * from etype where typeid = '" & txtID.Text.Replace("'", "") & "'").Tables(0).Rows.Count > 0 Then
            btnNew.Text = "Update"
            txtName.Text = db.getvalue("select * from etype where typeid='" & txtID.Text.Replace("'", "") & "'", "typename")

        Else
            btnNew.Text = "New"
            txtName.Text = ""
        End If
    End Sub

    Private Sub dlgDepartment_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dgvType.DataSource = db.getvalue("select * from etype").Tables(0)
        dgvType.Columns(0).Width = 50
        dgvType.Columns(1).Width = 100
        dgvType.Columns(2).Width = 180
    End Sub

    Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
        Dim sql As String = ""
        If txtID.Text.Length <= 0 Then
            MsgBox("ID is blank!!!")
            Exit Sub
        End If
        If txtName.Text.Length <= 0 Then
            MsgBox("Name is blank!!!")
            Exit Sub
        End If
        If btnNew.Text.ToLower = "new" Then
            sql = "insert EType(typeid,typename) values('" & txtID.Text.Replace("'", "") & "','" & txtName.Text.Replace("'", "") & "')"

        End If
        If btnNew.Text.ToLower = "update" Then
            sql = "update etype set typename='" & txtName.Text.Replace("'", "") & "' where typeid='" & txtID.Text.Replace("'", "") & "'"
        End If
        Try
            db.update(sql)
            dgvType.DataSource = db.getvalue("select * from etype").Tables(0)
            dgvType.Columns(0).Width = 50
            dgvType.Columns(1).Width = 100
            dgvType.Columns(2).Width = 180
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            If dgvType.Rows.Count > 0 Then
                db.updateTable(dgvType.DataSource, "select typeid,typename,typedesc from etype")
                MsgBox("Saved!!!", MsgBoxStyle.Information)
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        
    End Sub
End Class
